package com.jv.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jv.entity.*;
import com.jv.service.IBigTypeService;
import com.jv.service.IBigTypeService;
import com.jv.service.ISmallTypeService;
import com.jv.util.DateUtil;
import com.jv.util.StringUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理-商品大类Controller
 * @author Jv
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/bigType")
public class AdminBigTypeController {
    @Autowired
    private IBigTypeService bigTypeService;
    @Autowired
    private ISmallTypeService smallTypeService;
    @Value("${bigTypeImagesFilePath}")
    private String bigTypeImagesFilePath;  //大类图片地址

    //根据条件分页查询商品大类信息
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query = pageBean.getQuery().trim();
        Page<BigType> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<BigType> pageResult = bigTypeService.page(page, new QueryWrapper<BigType>().like(StringUtil.isNotEmpty(query), "name", query));
        HashMap<String, Object> map = new HashMap<>();
        map.put("bigTypeList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    //添加或修改商品大类
    @RequestMapping("/save")
    public R save(@RequestBody BigType bigType){
        if (bigType.getId()==null || bigType.getId()==-1) {
            bigTypeService.save(bigType);//添加
        }else { //修改
            bigTypeService.saveOrUpdate(bigType);
        }
        return R.ok();
    }

    //根据id查询
    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        BigType bigType = bigTypeService.getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("bigType",bigType);
        return R.ok(map);
    }

    //删除大类
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id){
        //*大类下面有小类的 不可删除 返回报错
        if (smallTypeService.count(new QueryWrapper<SmallType>().eq("bigTypeId",id))>0){
            return R.error(500,"大类下面有小类信息，不能删除");
        }else {
            bigTypeService.removeById(id);
            return R.ok();
        }
    }

    //上传商品大类图片
    @RequestMapping("/uploadImage")
    public Map<String ,Object> uploadImage(MultipartFile file)throws Exception{
        HashMap<String, Object> resultMap = new HashMap<>();
        if (!file.isEmpty()) {
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀  .jpg
            String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffixName;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(bigTypeImagesFilePath+newFileName));
            resultMap.put("code",0);
            resultMap.put("msg","上传成功");
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","/image/bigType/"+newFileName);
            resultMap.put("data",dataMap);
        }
        return resultMap;
    }

    //查询所有数据，下拉框用到
    @RequestMapping("/listAll")
    public R listAll(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("bigTypeList",bigTypeService.list());
        return R.ok(map);
    }
}
